package com.kcsj.service;
//201902104050 姜瑞临
import com.kcsj.dao.TaskListDao;
import com.kcsj.domain.TaskList;
import com.kcsj.domain.user.Notice;
import com.kcsj.domain.user.User;
import com.kcsj.service.user.UserService;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class TaskListService {
    private static TaskListDao taskListDao = TaskListDao.getInstance();
    private static TaskListService taskListService = new TaskListService();

    public TaskListService() {
    }

    public static TaskListService getInstance() {
        return taskListService;
    }

    public boolean add(TaskList taskList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean confirm = taskListDao.add(taskList, conn);
        conn.close();
        return confirm;
    }

    public boolean delete(String taskListNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean confirm = taskListDao.delete(taskListNo, conn);
        conn.close();
        return confirm;
    }

    public boolean update(TaskList taskList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        int id = taskList.getEmployee().getId();
        User user = UserService.getInstance().find(id);
        Notice notice = new Notice("配送员",
                "任务单有变更,任务单号："+taskList.getTaskListNo(),"未读",user);
        NoticeService.getInstance().add(notice);
        boolean confirm = taskListDao.update(taskList, conn);
        conn.close();
        return confirm;
    }

    public TaskList find(String taskListNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        TaskList taskList = taskListDao.find(taskListNo, conn);
        conn.close();
        return taskList;
    }

    public Collection<TaskList> findAll(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<TaskList> taskLists =taskListDao.findAll(id,conn);
        conn.close();
        return taskLists;
    }
}
